package com.augurit.gzcgwsp.ywcmn.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.gzcgwsp.ywcmn.convert.YwDzgzListConvertor;
import com.augurit.gzcgwsp.ywcmn.dao.YwDzgzListDao;
import com.augurit.gzcgwsp.ywcmn.entity.YwDzgzList;
import com.augurit.gzcgwsp.ywcmn.service.IYwDzgzListService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwDzgzListForm;

@Service
@Transactional
public class YwDzgzListServiceImpl implements IYwDzgzListService{

	@Autowired
	private YwDzgzListDao ywDzgzListDao;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public YwDzgzListForm get(Long id) {
		YwDzgzList entity = ywDzgzListDao.get(id);
		return YwDzgzListConvertor.convertVoToForm(entity);
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		ywDzgzListDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(YwDzgzListForm... forms) {
		if(forms != null)
			for(YwDzgzListForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(YwDzgzListForm form){
		
		if(form != null){
			YwDzgzList entity = null;
			
			//准备VO对象
			if(form != null && form.getId() != null){
				entity = ywDzgzListDao.get(form.getId());
			}else{
				entity = new YwDzgzList();
			}
			//属性值转换
			YwDzgzListConvertor.convertFormToVo(form, entity);
			
			System.out.println(entity.getUsername()+"----------------------");
			//保存
			ywDzgzListDao.save(entity);
			
			//回填ID属性值
			form.setId(entity.getId());
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<YwDzgzListForm> search(Page<YwDzgzListForm> page, YwDzgzListForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from YwDzgzList ps where 1=1 and ps.ajid='741'");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		ywDzgzListDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<YwDzgzListForm> list = YwDzgzListConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	/**
	 *  根据案件id查找
	 * @param page
	 * @param ajid
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<YwDzgzListForm> search(Page<YwDzgzListForm> page, Long ajid) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		pg.setPageSize(100);
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from YwDzgzList ps where 1=1 and ps.ajid='"+ajid+"'");
		List values = new ArrayList();
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		ywDzgzListDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<YwDzgzListForm> list = YwDzgzListConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<YwDzgzListForm> search(Page<YwDzgzListForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<YwDzgzList> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		ywDzgzListDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<YwDzgzListForm> list = YwDzgzListConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	@Override
	public YwDzgzListForm getFromByCode(Long ajid, String templateCode,String filename) {
	    String hql=" from YwDzgzList t where t.ajid="+ajid+" and templateCode='"+templateCode+"'" +
	    		" and filename='"+filename+"'";
	    List<YwDzgzList> list=this.ywDzgzListDao.find(hql);
	    if(list.size()>0){
	    	return YwDzgzListConvertor.convertVoToForm(list.get(0));
	    }
	    
		return null;
	}
}
